<template>
  <div>
    <!-- 自动派单 2018/6/14 ZuoW -->
    <Row :style="{minHeight:200+'px'}">
      <i-col push="1" span="22">
        <Form ref="autoSendNoticeForm" :model="autoSendNoticeForm" label-position="left" :label-width="100">
          <Row>
            <i-col span="11">
              <FormItem label="开始时间：">
                <TimePicker v-model="myTimeStart" type="time" placeholder="开始时间"></TimePicker>
              </FormItem>
            </i-col>
            <i-col push="1" span="11">
              <FormItem label="结束时间：">
                <TimePicker v-model="myTimeEnd" type="time" placeholder="结束时间"></TimePicker>
              </FormItem>
            </i-col>
          </Row>
          <FormItem label="派单频率：">
            <RadioGroup v-model="autoSendNoticeForm.dispatchFrequency">
              <Radio label="1">仅此一次</Radio>
              <Radio label="2">每天</Radio>
            </RadioGroup>
          </FormItem>

          <FormItem label="派单方式：">
            <Select v-model="autoSendNoticeForm.dispatchType" style="width:240px;">
              <Option value="1">整列派单</Option>
              <Option value="2">按收货人派单</Option>
            </Select>
          </FormItem>
        </Form>
      </i-col>
    </Row>
    <div class="xlinkModal-footer">
      <Button type="primary" @click="submitAutoDispatcher">确认</Button>
      <Button @click="cancel">取消</Button>
    </div>
  </div>
</template>

<script>
  import {autoDispatcher, detectAutoDispatcherConfig} from '@api/backEnd/mainOrder/index'
  import {mapGetters} from 'vuex'
  import moment from 'moment'

  export default {
    name: 'dispatchToFleet',
    data () {
      let myTimeStart = ''
      let myTimeEnd = ''
      let autoSendNoticeForm = {
        userId: '',
        beginTime: '',
        endTime: '',
        dispatchFrequency: '',
        dispatchType: '',
        userType: 1
      }
      let datas = {
        myTimeStart, myTimeEnd, autoSendNoticeForm
      }
      return datas
    },
    methods: {
      cancel () {
        this.$emit('close')
      },
      submitAutoDispatcher () {
        this.autoSendNoticeForm.userId = this.currentUser.userId
        if (!this.autoSendNoticeForm.beginTime) {
          this.$Notice.warning({
            title: '错误',
            desc: '开始时间必填'
          })
          return false
        }
        if (!this.autoSendNoticeForm.endTime) {
          this.$Notice.warning({
            title: '错误',
            desc: '结束时间必填'
          })
          return false
        }
        if (!this.autoSendNoticeForm.dispatchFrequency) {
          this.$Notice.warning({
            title: '错误',
            desc: '派单频次必选'
          })
          return false
        }
        if (!this.autoSendNoticeForm.dispatchType) {
          this.$Notice.warning({
            title: '错误',
            desc: '派单方式必选'
          })
          return false
        }
        detectAutoDispatcherConfig().then(res => {
          if (+res.data.isSuccess === 1) {
            autoDispatcher(this.autoSendNoticeForm).then(res => {
              if (+res.data.isSuccess === 1) {
                this.$Notice.success({
                  title: '成功',
                  desc: '自动派单添加成功'
                })
                this.$emit('close')
              } else {
                this.$Notice.warning({
                  title: '失败',
                  desc: res.data.res
                })
              }
            })
          } else {
            this.$Notice.warning({
              title: '失败',
              desc: res.data.res
            })
          }
        })
      }
    },
    created () {
    },
    mounted () {
    },
    watch: {
      myTimeStart: function (oldVal, newVal) {
        this.autoSendNoticeForm.beginTime = moment().format('YYYY-MM-DD') + ' ' + this.myTimeStart
      },
      myTimeEnd: function (oldVal, newVal) {
        this.autoSendNoticeForm.endTime = moment().format('YYYY-MM-DD') + ' ' + this.myTimeEnd
      }
    },
    computed: {
      ...mapGetters(['currentUser'])
    },
    components: {}
  }
</script>

<style scoped>

</style>
